import {Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { MenuTreeComponent } from './menus-tree.component';
import { AddMenuComponent } from './add-menu.component';
import { MenuCleanServiceProxy, MenuItemDto, NavigationServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { SyncMenusModalComponent } from './sync-menus-modal.component';

@Component({
    templateUrl: './menus.component.html',
    animations: [appModuleAnimation()]
})
export class MenusComponent extends AppComponentBase {
    @ViewChild('menuTree', { static: true }) menuTree: MenuTreeComponent;
    @ViewChild('addMenu', { static: true }) addMenu: AddMenuComponent;
    @ViewChild('syncMenus', { static: true }) syncMenus: SyncMenusModalComponent;

    saving = false;
    dataSynchronous = false;
    constructor(
        injector: Injector,
        private _menuCleanService: MenuCleanServiceProxy,
        private _navigationService: NavigationServiceProxy,
    ) {
        super(injector);

        this.dataSynchronous = this.setting.getBoolean("App.Synchronous.DataSynchronous");
        if(this.dataSynchronous){
            this.dataSynchronous = !this.appSession.motherSiteInfo ? false: this.appSession.motherSiteInfo.motherTenantId !== this.appSession.tenantId;
        }
    }

    addingMenu(event: MenuItemDto): void {
        this.addMenu.menuItemDto = new MenuItemDto();
        if(event){
            this.addMenu.menuItemDto.parentId = event.id;
            this.addMenu.parentDisplayName = event.displayName;
        }
    }

    editMenu(event: MenuItemDto):void{
        this.addMenu.menuItemDto = event;
    }

    menuCreated(event: MenuItemDto):void{
        this.menuTree.menuCreated(event);
    }

    menuUpdated(event: MenuItemDto):void{
        this.menuTree.menuUpdated(event);
    }

    
    //重置栏目缓存
    resetMenusCache():void{
        this.saving = true;
        this._menuCleanService.resetMenuCache()
        .pipe(finalize(() => { this.saving = false; }))
        .subscribe(() => {
            this.notify.success(this.l('CacheSuccessfullyCleared'));
        });
    }
    //重置路由缓存
    resetRouteCache():void{
        this.saving = true;
        this._menuCleanService.resetCustomRouteCache()
        .pipe(finalize(() => { this.saving = false; }))
        .subscribe(() => {
            this.notify.success(this.l('CacheSuccessfullyCleared'));
        });
    }

    // 导入的栏目数据有脏数据，导致部分功能无法正常执行
    cleanMenusData(){
        this.saving = true;
        this._menuCleanService.cleanMenus()
        .pipe(finalize(() => { this.saving = false; }))
        .subscribe((result) => {
            this.notify.success(this.l("{0}dataHaveHeenDeleted", result));
        });
    }

    // 重置code和order
    resetCode():void{
        this.saving = true;
        this._menuCleanService.resetCode()
        .pipe(finalize(() => { 
            this.reload();
            this.saving = false; 
        }))
        .subscribe(() => {
            this.notify.success(this.l('ResetSuccessfully'));
        });
    }

    ///批量生成路由
    batchRoute(): void {
        this.saving = true;
        this._navigationService.routeBatchGeneration()
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe((result) => {

                let contentStr = '';
                let data = result.canNotGenerated;
                for (let i = 0; i < data.length; i++) {
                    contentStr = contentStr + data[i].displayName + ", " + data[i].code + "<br>";
                }
                this.message.info(
                    null,
                    this.l('Message'),
                    this.l('RouteHaveHeenGenerated', result.hadGenerated, contentStr)
                ).done(function () {
                    if (result.hadGenerated > 0) {
                        this.reload();
                    }
                });
            });
    }

    synchorization(){
        this.syncMenus.show();
    }

    reload(){
        this.menuTree.reload();
    }
}
